package com.sincetimes.game.tips.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.sincetimes.game.Constant;
import com.sincetimes.game.util.CSVManager;
import com.sincetimes.game.util.ConfigManager;

public class Tips extends AbstractController{
	private static Logger log=Logger.getLogger("tipsLog");
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		JSONObject json =new JSONObject();
		String ver=req.getParameter("ver");
		String version=req.getParameter("updateversion");
		String plat=req.getParameter("platform");
		String curver=null;
		String status="0";
		String resource=null;
		log.info("tips,begin,"+ver+","+plat+","+version+","+req.getRemoteAddr());
		String isdebug=ConfigManager.getInstance().getConfig("isdebug");
		String ip=req.getRemoteAddr();

		curver=ConfigManager.getInstance().getConfig(Constant.VER);
		status=ConfigManager.getInstance().getConfig(Constant.TIPS_STATUS);
		resource=ConfigManager.getInstance().getConfig(plat+"_"+Constant.RES_RELEASE);
		
		String msg = ConfigManager.getInstance().getConfig(Constant.TIPS_MSG);
		
		if(ver!=null&&!"".equals(ver))
		{
			try{
				json.put("ver", curver);
				int vernum=Integer.parseInt(ver.trim());
				int curvernum=Integer.parseInt(curver.trim());
				log.info("vernum : " + vernum);
				log.info("curvernum : " + curvernum);
				
				// 服务器选择业务取消
				if(vernum>=curvernum){
					json.put("paygamesvr","0");
					json.put("msg", msg);
					json.put("address", ConfigManager.getInstance().getConfig(Constant.ADDRESS_RELEASE));
					
					if(isDebugEnvi(ip)){
						
						if(version!=null&&!"".equals(version)){
							json.put("cdn", getVersion(ver,version,plat,false));
						}						
						
						if ("2".equals(status) || "0".equals(status)) {
							json.put("open","0");
						}
						else {
							json.put("open","1");
						}						
						
						json.put("resource",ConfigManager.getInstance().getConfig(plat+"_"+Constant.RES_DEBUG));
						json.put("address", ConfigManager.getInstance().getConfig(Constant.ADDRESS_DEBUG));
					}else{
						if(version!=null&&!"".equals(version)){
							json.put("cdn", getVersion(ver,version,plat,true));
						}
						json.put("resource", resource);
						
						if ("0".equals(status)) {
							json.put("open","0");
						}
						else {
							json.put("open","1");
						}	
					}
				}
				// 强制更新
				else if (vernum < curvernum) {
					json.put("open","1");
					json.put("msg", "亲爱的玩家爱您好，游戏有了最新版本，请下载最新版本之后进入游戏，以免造成数据异常");
					json.put("address", ConfigManager.getInstance().getConfig(Constant.ADDRESS_RELEASE));
				}
				
				// 服务器地址通过配表获取
				Boolean flag = isdebug.equals("1");
				String url = CSVManager.getInstance().getIp(vernum, flag);
				String port = CSVManager.getInstance().getPort(vernum, flag);
				json.put("resurl", url + ":" + port + "/");
				
			}catch(NumberFormatException e){
				json.put("open",status );
				json.put("msg", e.getMessage());
				log.info("tips,error,"+e.getMessage());
				return null;
			}
		}else{
			json.put("open",1 );
			json.put("msg", msg);
		}
		resp.setContentType("text/xml");
		resp.setCharacterEncoding("UTF-8");
		resp.getWriter().print(json);
		resp.getWriter().flush();
		resp.getWriter().close();
		return null;
	}
	
	public boolean isDebugEnvi(String ip) {
			
		return "210.82.98.45".equals(ip) || "210.82.46.69".equals(ip);
		
	}
	
	public String getVersion(String bigver,String version,String platform,boolean isRelease){
		if(isRelease){
			String updateversion=ConfigManager.getInstance().getConfig(Constant.UPDATEVERSION_RELEASE);
			String resurl=ConfigManager.getInstance().getConfig(platform+"_"+Constant.RES_RELEASE);
			Map<String, String> kvMap=new HashMap<String, String>();
			String[] ver=updateversion.split(";");
			if(ver.length>0){
				for(int i=0;i<ver.length;i++){
					kvMap.put(bigver+"_"+ver[i].split(",")[0], ver[i].split(",")[1]);
				}
				if(kvMap.containsKey(bigver+"_"+version)){
					return resurl+kvMap.get(bigver+"_"+version);
				}else{
					return resurl+kvMap.get(bigver+"_1");
				}				
			}
			
		}else {
			String updateversion=ConfigManager.getInstance().getConfig(Constant.UPDATEVERSION_DEBUG);
			String resurl=ConfigManager.getInstance().getConfig(platform+"_"+Constant.RES_DEBUG);
			Map<String, String> kvMap=new HashMap<String, String>();
			String[] ver=updateversion.split(";");
			if(ver.length>0){
				for(int i=0;i<ver.length;i++){
					kvMap.put(bigver+"_"+ver[i].split(",")[0], ver[i].split(",")[1]);
				}
				if(kvMap.containsKey(bigver+"_"+version)){
					return resurl+kvMap.get(bigver+"_"+version);
				}else{
					return resurl+kvMap.get(bigver+"_1");
				}
			}
		}
		return null;
	}
	
	
	public static  void main(String[] args){
		String ver="v11a ";
		int vernum=Integer.parseInt(ver.trim().substring(1));
		System.out.println(vernum);
	}
}